﻿using SmartSpace.UI.Silverlight;
using SmartSpace.UI.Silverlight.Theme;
using System.Windows.Media.Imaging;
using System;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Browser;

namespace SmartSpace.UI.Silverlight.Themes.ScienceFiction
{
    public partial class MainPanel : SlView, IMainPanel
    {
        #region Variables

        private static double ScienceFictionWorkAreaHeight = 500;

        private static double TotalRedundantHeight = 0;

        #endregion

        public MainPanel()
        {
            InitializeComponent();
            m_gridViewContainer.Visibility = Visibility.Collapsed;

            m_navigatePanel.NavigationPanelCollapsed += new Action<bool>(m_navigatePanel_NavigationPanelCollapsed);
            //m_alertMessagePanel.NavigationPanelCollapsed += new System.Action<bool>(OnAlertMessagePanelCollapsed);
            m_workAreaPanel.SwitchExEvent += new System.Action<SlView, int>(OnWorkAreaPanelSwitchExEvent);

            m_headerPanel.FullScreenEventHandler += new System.Action<bool>(OnHeaderPanelFullScreenEventHandler);

            Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
        }

        void m_navigatePanel_NavigationPanelCollapsed(bool obj)
        {
            if (obj)
            {
                LayoutWorkArea.ColumnDefinitions[0].Width = new GridLength(0, GridUnitType.Auto);
            }
            else
            {
                LayoutWorkArea.ColumnDefinitions[0].Width = new GridLength(160, GridUnitType.Pixel);
            }
        }

        void Content_Resized(object sender, EventArgs e)
        {
            SetWordAreaHeight(true);
        }

        protected override void OnShown()
        {
            if (IsFirstDisplay)
            {
                m_workAreaPanel.Init();
                SetWordAreaHeight(true);
            }
            //m_alertMessagePanel.GoToCollapsed();
        }

        void OnWorkAreaPanelSwitchExEvent(SlView obj, int index)
        {
            if (index == 1)
            {
                m_viewContainer.SwitchView(obj);
            }
            else
            {
                (obj as AppView).CustomEvent += new Action<object>(MainPanel_CustomEvent);
                m_containerMessage.SwitchView(obj);
            }
        }

        void MainPanel_CustomEvent(object obj)
        {
            m_popupMessage.Visibility = System.Windows.Visibility.Visible;
        }

        private void m_btnHideMessage_Click(object sender, RoutedEventArgs e)
        {
            m_popupMessage.Visibility = System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// 单击标题栏控件全屏显示按钮后的操作
        /// </summary>
        /// <param name="obj"></param>
        void OnHeaderPanelFullScreenEventHandler(bool obj)
        {
            SetNoticePanelCollapsed();
            OnHeaderPanelCollapsed(obj);
            if (obj)
            {
                m_navigatePanel.OnButtonCollapseClick(null, null);             
            }
            else
            {
                m_navigatePanel.OnButtonExpandedClick(null, null);
            }

            SetWordAreaHeight(obj);
        }

        /// <summary>
        /// 页面标题栏的显示隐藏事件
        /// </summary>
        /// <param name="obj"></param>
        void OnHeaderPanelCollapsed(bool obj)
        {
            if (obj)
            {
                //TODO 不是很好的修改方法
                borderLabel.Visibility = Visibility.Collapsed;
                LayoutHeader.Height = LayoutHeader.ActualHeight - 90;
                m_headerPanel.Height = 23;

                VisualStateManager.GoToState(borderLabel, "Collapsed", false);
            }
            else
            {
                borderLabel.Visibility = Visibility.Visible;
                LayoutHeader.Height = LayoutHeader.ActualHeight + 90;
                m_headerPanel.Height = 113;

                VisualStateManager.GoToState(borderLabel, "Expanded", false);
            }
            LayoutHeader.UpdateLayout();
        }

        #region IMainPanel Members

        public INavigatePanel GetNavigatePanel()
        {
            return m_navigatePanel;
        }

        public IWorkAreaPanel GetWorkAreaPanel()
        {
            return m_workAreaPanel;
        }

        public IHeaderPanel GetHeaderPanel()
        {
            return m_headerPanel;
        }

        public SlView GetContentView()
        {
            return this;
        }

        #endregion

        /// <summary>
        /// 显示隐藏公告栏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnGridMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (m_gridViewContainer.Visibility == System.Windows.Visibility.Visible)
            {
                m_gridViewContainer.Visibility = System.Windows.Visibility.Collapsed;
                //m_imageCollape.Source = new BitmapImage(new Uri("/SmartSpace.UI.Silverlight.Themes.ScienceFiction;component/Resources/Notice.PNG", UriKind.RelativeOrAbsolute));
                //ToolTipService.SetToolTip(m_imageCollape, "点击显示通知公告栏");
            }
            else
            {
                m_gridViewContainer.Visibility = System.Windows.Visibility.Visible;
                //m_imageCollape.Source = new BitmapImage(new Uri("/SmartSpace.UI.Silverlight.Themes.ScienceFiction;component/Resources/Notice.PNG", UriKind.RelativeOrAbsolute));
                //ToolTipService.SetToolTip(m_imageCollape, "点击隐藏通知公告栏!");
            }
        }

        /// <summary>
        /// 设置工作区域的高度
        /// </summary>
        private void SetWordAreaHeight(bool isFullScreen)
        {
            LayoutRoot.Height = Application.Current.Host.Content.ActualHeight;// -m_imageCollape.ActualHeight;
            ////设置工作区域高度
            //ScriptObject screen = (ScriptObject)HtmlPage.Window.GetProperty("screen");

            //if (screen != null)
            //{
            //    double clientHeight = (double)HtmlPage.Document.Body.GetProperty("clientHeight");

            //    double headerHeight = LayoutHeader.ActualHeight;
            //    double bottomHeight = LayoutBottom.ActualHeight;

            //    m_workAreaPanel.Height = clientHeight - bottomHeight - headerHeight - TotalRedundantHeight;
            //    LayoutRoot.UpdateLayout();
            //}
            //else
            //{
            //    m_workAreaPanel.Height = ScienceFictionWorkAreaHeight;
            //}
        }

        /// <summary>
        /// 隐藏通知公告信息详情
        /// </summary>
        /// <param name="isVisible"></param>
        private void SetNoticePanelCollapsed()
        {
            m_gridViewContainer.Visibility = System.Windows.Visibility.Collapsed;
            //ToolTipService.SetToolTip(m_imageCollape, "点击显示通知公告栏");
        }
    }
}
